package com.help10000.rms.mvp.presenter

import android.util.Log
import com.amir.common.api.CustomObserver
import com.amir.common.beans.OrderWrapperEntity
import com.amir.common.mvp.model.Model
import com.amir.common.utils.Constant
import com.amir.common.utils.SPUtils
import com.amir.common.utils.ToastUtil
import com.amir.common.utils.Utils
import com.help10000.rms.mvp.view.OrderView
import com.help10000.rms.mvp.view.RobOrderView
import com.help10000.rms.utils.LocationManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class RobOrderPresenter(view: OrderView) : OrderPresenter(view) {

    /**
     * 获取抢单数据
     */
    fun getRobOrderData() {

        val accessToken = SPUtils.getString(Constant.AccessToken)
        val cmpyCode = SPUtils.getString(Constant.CmpyCode)
        CoroutineScope(Dispatchers.IO).launch {
            val (lat, lng) = LocationManager.getLocation(Utils.getContext())
            Log.d("获取抢单数据实时定位", "获取到经纬度：lat=$lat, lng=$lng")
            // 切换到主线程处理定位结果

            val latitude = lat
            val longitude = lng
            val robOrderListAPi = Model.getServer()
                .robOrderListAPi(accessToken, cmpyCode, lat = latitude, lng = longitude)

            Model.getObservable(
                robOrderListAPi,
                object : CustomObserver<OrderWrapperEntity>(mvpView) {

                    override fun success(t: OrderWrapperEntity) {
                        mvpView?.onDataSuccess(t.data.OrderList);
                    }
                })
        }
    }



    override fun getMvpView(): RobOrderView {
        return super.getMvpView() as RobOrderView
    }
}
